const user_model = require('../db/user_model')
const jwt = require('jsonwebtoken')
const jwt_config = require('../config/jwt_config')

const login = async (ctx) => {
  
  const {username, password} = ctx.request.body
  try {
    let user = await user_model.user_select_by_username([['id', 'password', 'username','stu_name','img_url'], username])
    if (user.length) {
      user = user[0]
      if (user.password === password) {
        const token_user = {username: user.username, id: user.id,stu_name:user.stu_name,img_url:user.img_url}        
        const token = jwt.sign(token_user, jwt_config.secret, {expiresIn: '2h'})
        ctx.body = {
          user: token_user,
          token
        }
      } else {
        ctx.throw(400, '用户名或密码错误')
      }
    } else {
      ctx.throw(400, '用户名不存在')
    }
  } catch (error) {
    ctx.throw(400, error)
  }
}

const login_admin = async (ctx) => {
  const {username, password} = ctx.request.body
  try {
    let user = await user_model.user_selectadmin_by_username([['id', 'password', 'username','stu_name','img_url'], username])
    if (user.length) {
      user = user[0]
      if (user.password === password) {
        const token_user = {username: user.username, id: user.id,stu_name:user.stu_name,img_url:user.img_url}        
        const token = jwt.sign(token_user, jwt_config.secret, {expiresIn: '2h'})
        ctx.body = {
          user: token_user,
          token
        }
      } else {
        ctx.throw(400, '用户名或密码错误')
      }
    } else {
      ctx.throw(400, '用户名不存在')
    }
  } catch (error) {
    ctx.throw(400, error)
  }
}

const register = async (ctx) => {
  const user =  {username, password, nikiname, email} = ctx.request.body

  const checkEmail = await user_model.user_check_email(email)
  // console.log(checkEmail)
  if (checkEmail.length) {
    ctx.throw(400, '邮箱被占用')
  } else {
    const res = await user_model.user_insert(user)
    ctx.body = "注册成功"
  }
  
}

const get_user = async (ctx) => {
  const {id} = ctx.params
  try {
    const user = await user_model.user_select_by_id(id)
    ctx.body = user
  } catch (error) {
    ctx.throw(400, error.code)
  }
}

const get_user_data = async (ctx) => {
  const {id} = ctx.params
  try {
    const user = await user_model.user_selectData_by_id(id)
    ctx.body = user
  } catch (error) {
    ctx.throw(400, error.code)
  }
}

// 证书上传
const credential = async (ctx) => {
  const credentials = {grade,user_id,apply_title,cret_url} = ctx.request.body
  try {
    const result = await user_model.credential_insert(credentials)
    // console.log(result)
    // ctx.body = apply_model.apply_insert
    ctx.body = result
  } catch (error) {
    ctx.throw(400, error)
  }
};

  // 验证密码
  const yz_login = async (ctx) => {
    const {username, password} = ctx.request.body
    try {
      let user = await user_model.user_select_by_username([['id', 'password', 'username','stu_name'], username])
      if (user.length) {
        user = user[0]
        if (user.password === password) {
          const token_user = {username: user.username, id: user.id,stu_name:user.stu_name}        
          const token = jwt.sign(token_user, jwt_config.secret, {expiresIn: '2h'})
          ctx.body = {
            user: token_user,
            token
          }
        } else {
          ctx.throw(400, '用户名或密码错误')
        }
      } else {
        ctx.throw(400, '用户名不存在')
      }
    } catch (error) {
      ctx.throw(400, error)
    }
  }

  // 更改用户密码
  const updated_password = async (ctx) => {
    const newPaw = {username, password} = ctx.request.body
    try {
      let user_up = await user_model.user_updata(newPaw)
      ctx.body = user_up
    } catch (error) {
      ctx.throw(400, error)
    }
  }

  const delete_user_id =  async (ctx) => {
    const {id} = ctx.request.body
    try {
      const norices = await user_model.user_delete_by_id(id)
      ctx.body = norices
    } catch (error) {
      ctx.throw(400, error)
    }
  }




const auth_token = async (ctx) => {
  ctx.body = '已登录'
}
module.exports = {
  login,
  register,
  get_user,
  auth_token,
  get_user_data,
  credential,
  yz_login,
  updated_password,
  delete_user_id,
  login_admin
}
